﻿using System;

namespace Common.Xml
{
    /// <summary>
    /// 输出基类文件
    /// </summary>
    public class CreateClassFile
    {
        public string WriteCsFile(string projectName)
        {
            System.Collections.Generic.List<T_Table> _tables = XMLReader.GetTables();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (T_Table _table in _tables)
            {
                sb.AppendLine("using System;");
                sb.AppendLine("using System.Collections.Generic;");
                sb.AppendLine("using System.Linq;");
                sb.AppendLine("namespace " + projectName);
                sb.AppendLine("{");
                sb.AppendLine("    /// <summary>");
                sb.AppendLine("    /// " + _table.Description);
                sb.AppendLine("    /// </summary>");
                sb.AppendLine("    public class " + _table.TableName);
                sb.AppendLine("    {");
                System.Collections.Generic.List<T_Field> fields = XMLReader.GetFields(_table.TableName);
                sb.AppendLine("");
                sb.AppendLine("        #region 属性");
                sb.AppendLine("");
                for (int i = 0; i < fields.Count; i++)
                {
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// " + fields[i].FieldName);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        public " + fields[i].Attribute + "  " + fields[i].Field + " { get; set; }");
                    sb.AppendLine("");
                }

                if (_table.IsStatic == true)
                {
                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// " + _table.Description + "锁的对象");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine("object obj = new object();");
                }

                sb.AppendLine("");
                sb.AppendLine("        #endregion");
                sb.AppendLine("");
                sb.AppendLine("        #region 一些通用的方法");
                if (_table.IsStatic == false)
                {
                    sb.AppendLine("");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 得到所有 " + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>所有" + _table.Description + "</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetList()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetList(field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 分页显示" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"PageSize\">每页记录数</param>");
                    sb.AppendLine("        /// <param name=\"NowPage\">当前页</param>");
                    sb.AppendLine("        /// <param name=\"PageCount\">总共多少页</param>");
                    sb.AppendLine("        /// <param name=\"recordCount\">总记录数</param>");
                    sb.AppendLine("        /// <returns>查询到的记录</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetListByPage(PageSize, NowPage, out PageCount, out recordCount, field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 分页模糊查询" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"PageSize\">每页记录数</param>");
                    sb.AppendLine("        /// <param name=\"NowPage\">当前页</param>");
                    sb.AppendLine("        /// <param name=\"PageCount\">总共多少页</param>");
                    sb.AppendLine("        /// <param name=\"recordCount\">总记录数</param>");
                    sb.AppendLine("        /// <returns>查询到的记录</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetListByPageLikeKey(int PageSize, int NowPage, out int PageCount, out int recordCount)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetListByPageLikeKey(PageSize, NowPage, field, out PageCount, out recordCount);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 分页查询" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"PageSize\">每页记录数</param>");
                    sb.AppendLine("        /// <param name=\"NowPage\">当前页</param>");
                    sb.AppendLine("        /// <param name=\"PageCount\">总共多少页</param>");
                    sb.AppendLine("        /// <param name=\"recordCount\">总记录数</param>");
                    sb.AppendLine("        /// <returns>查询到的记录</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetListByPageAndKeys(int PageSize, int NowPage, out int PageCount, out int recordCount)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetListByPageAndKeys(PageSize, NowPage, field, out PageCount, out recordCount);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 查询所有关键字=？");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"key\">字段</param>");
                    sb.AppendLine("        /// <param name=\"value\">值</param>");
                    sb.AppendLine("        /// <returns>对象的List</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetAllByKey(string key, string value)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetAllByKey(key, value, field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 得到一个" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>对象</returns>");
                    sb.AppendLine("        public " + _table.TableName + " GetModel()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            field.Id = this.Id;");
                    sb.AppendLine("            return (" + _table.TableName + ")Common.ADO.DBManager.GetModel(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 保存一个" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>编号</returns>");
                    sb.AppendLine("        public int Insert()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            return Common.ADO.DBManager.Insert(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 按关键字查询头几条");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"count\">数量</param>");
                    sb.AppendLine("        /// <returns>" + _table.Description + "的列表</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetTopList(int count)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetTopList(count, field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 判断" + _table.Description + "是否唯一存在");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>是否存在</returns>");
                    sb.AppendLine("        public bool IsExist()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            return Common.ADO.DBManager.IsExist(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 判断" + _table.Description + "是否存在");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>是否存在</returns>");
                    sb.AppendLine("        public bool IsJustExist()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            return Common.ADO.DBManager.IsJustExist(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 得到关键字等于。。。的所有" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>" + _table.Description + "的List</returns>");
                    sb.AppendLine("        public List<" + _table.TableName + "> GetSomeObject()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetSomeObject(field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 编辑一个" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"obj\">对象实体</param>");
                    sb.AppendLine("        public void Edit()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            Common.ADO.DBManager.Edit(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 删除一个" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        public void Delete()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            field.Id = this.Id;");
                    sb.AppendLine("            Common.ADO.DBManager.Delete(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 删除所有" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        public void DeleteAll()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            Common.ADO.DBManager.DeleteAll(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 删除某些" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"fields\">" + _table.Description + "的数组</param>");
                    sb.AppendLine("        public void DeleteSome(" + _table.TableName + "[] fields)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            Common.ADO.DBManager.DeleteSome(fields);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 按关键字删除");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        public void DeleteColumByKey_Value()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            Common.ADO.DBManager.DeleteColumByKey_Value(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        private " + _table.TableName + " GetSelf()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    for (int i = 0; i < fields.Count; i++)
                    {
                        sb.AppendLine("field." + fields[i].Field + "= this." + fields[i].Field + ";");
                    }

                    sb.AppendLine("            return field;");
                    sb.AppendLine("        }");
                    sb.AppendLine("");
                }
                else
                {
                    sb.AppendLine("");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 得到所有 " + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>所有" + _table.Description + "</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetList()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetList(field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 分页显示" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"PageSize\">每页记录数</param>");
                    sb.AppendLine("        /// <param name=\"NowPage\">当前页</param>");
                    sb.AppendLine("        /// <param name=\"PageCount\">总共多少页</param>");
                    sb.AppendLine("        /// <param name=\"recordCount\">总记录数</param>");
                    sb.AppendLine("        /// <returns>查询到的记录</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetListByPage(PageSize, NowPage, out PageCount, out recordCount, field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 分页模糊查询" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"PageSize\">每页记录数</param>");
                    sb.AppendLine("        /// <param name=\"NowPage\">当前页</param>");
                    sb.AppendLine("        /// <param name=\"PageCount\">总共多少页</param>");
                    sb.AppendLine("        /// <param name=\"recordCount\">总记录数</param>");
                    sb.AppendLine("        /// <returns>查询到的记录</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetListByPageLikeKey(int PageSize, int NowPage, out int PageCount, out int recordCount)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetListByPageLikeKey(PageSize, NowPage, field, out PageCount, out recordCount);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 分页查询" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"PageSize\">每页记录数</param>");
                    sb.AppendLine("        /// <param name=\"NowPage\">当前页</param>");
                    sb.AppendLine("        /// <param name=\"PageCount\">总共多少页</param>");
                    sb.AppendLine("        /// <param name=\"recordCount\">总记录数</param>");
                    sb.AppendLine("        /// <returns>查询到的记录</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetListByPageAndKeys(int PageSize, int NowPage, out int PageCount, out int recordCount)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetListByPageAndKeys(PageSize, NowPage, field, out PageCount, out recordCount);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 查询所有关键字=？");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"key\">字段</param>");
                    sb.AppendLine("        /// <param name=\"value\">值</param>");
                    sb.AppendLine("        /// <returns>对象的List</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetAllByKey(string key, string value)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetAllByKey(key, value, field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 得到一个" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>对象</returns>");
                    sb.AppendLine("        private " + _table.TableName + " GetModel()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            field.Id = this.Id;");
                    sb.AppendLine("            return (" + _table.TableName + ")Common.ADO.DBManager.GetModel(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 保存一个" + _table.Description);
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>编号</returns>");
                    sb.AppendLine("        private int Insert()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            return Common.ADO.DBManager.Insert(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 按关键字查询头几条");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"count\">数量</param>");
                    sb.AppendLine("        /// <returns>" + _table.Description + "的列表</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetTopList(int count)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetTopList(count, field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 判断" + _table.Description + "是否唯一存在");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>是否存在</returns>");
                    sb.AppendLine("        private bool IsExist()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            return Common.ADO.DBManager.IsExist(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 判断" + _table.Description + "是否存在");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>是否存在</returns>");
                    sb.AppendLine("        private bool IsJustExist()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            return Common.ADO.DBManager.IsJustExist(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 得到关键字等于。。。的所有" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <returns>" + _table.Description + "的List</returns>");
                    sb.AppendLine("        private List<" + _table.TableName + "> GetSomeObject()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            List<object> objs = Common.ADO.DBManager.GetSomeObject(field);");
                    sb.AppendLine("            List<" + _table.TableName + "> fields = new List<" + _table.TableName + ">();");
                    sb.AppendLine("            foreach (" + _table.TableName + " _field in objs)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                fields.Add((" + _table.TableName + ")_field);");
                    sb.AppendLine("            }");
                    sb.AppendLine("            return fields;");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 编辑一个" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"obj\">对象实体</param>");
                    sb.AppendLine("        private void Edit()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            Common.ADO.DBManager.Edit(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 删除一个" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        private void Delete()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            field.Id = this.Id;");
                    sb.AppendLine("            Common.ADO.DBManager.Delete(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 删除所有" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        private void DeleteAll()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    sb.AppendLine("            Common.ADO.DBManager.DeleteAll(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 删除某些" + _table.Description + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        /// <param name=\"fields\">" + _table.Description + "的数组</param>");
                    sb.AppendLine("        private void DeleteSome(" + _table.TableName + "[] fields)");
                    sb.AppendLine("        {");
                    sb.AppendLine("            Common.ADO.DBManager.DeleteSome(fields);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 按关键字删除");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        private void DeleteColumByKey_Value()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = GetSelf();");
                    sb.AppendLine("            Common.ADO.DBManager.DeleteColumByKey_Value(field);");
                    sb.AppendLine("        }");
                    sb.AppendLine("        private " + _table.TableName + " GetSelf()");
                    sb.AppendLine("        {");
                    sb.AppendLine("            " + _table.TableName + " field = new " + _table.TableName + "();");
                    for (int i = 0; i < fields.Count; i++)
                    {
                        sb.AppendLine("field." + fields[i].Field + "= this." + fields[i].Field + ";");
                    }

                    sb.AppendLine("            return field;");
                    sb.AppendLine("        }");
                    sb.AppendLine("");

                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// 缓存" + _table.Description + "的集合");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine("private static List<" + _table.TableName + "> infos = null;");
                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// 得到静态存放的" + _table.Description + "信息，运行一次永远不用再次查询数据库");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine("/// <returns></returns>");
                    sb.AppendLine("public static List<" + _table.TableName + "> GetInstance()");
                    sb.AppendLine("{");
                    sb.AppendLine("if (infos == null)");
                    sb.AppendLine("{");
                    sb.AppendLine("infos = new " + _table.TableName + "().GetList();");
                    sb.AppendLine("}");
                    sb.AppendLine("return infos;");
                    sb.AppendLine("}");
                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// 保存一个" + _table.Description + "信息");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine("public int Insert" + _table.TableName + "()");
                    sb.AppendLine("{");
                    sb.AppendLine("int id = this.Insert();");
                    sb.AppendLine(_table.TableName + " info = new " + _table.TableName + "();");
                    sb.AppendLine("info = this.GetSelf();");
                    sb.AppendLine("info.Id = id;");
                    sb.AppendLine("lock (obj)");
                    sb.AppendLine("{");
                    sb.AppendLine("infos.Add(info);");
                    sb.AppendLine(" }");
                    sb.AppendLine("return id;");
                    sb.AppendLine("}");
                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// 得到一个" + _table.Description + "信息");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine("/// <returns></returns>");
                    sb.AppendLine("public " + _table.TableName + " GetOne" + _table.TableName + "()");
                    sb.AppendLine("{");
                    sb.AppendLine("List<" + _table.TableName + "> _infos = GetInstance();");
                    sb.AppendLine("var query = from _info in _infos where _info.Id == this.Id select _info;");
                    sb.AppendLine(_table.TableName + " _category = null;");
                    sb.AppendLine("if (query.Count() > 0)");
                    sb.AppendLine("{");
                    sb.AppendLine(" _category = query.First();");
                    sb.AppendLine(" }");
                    sb.AppendLine(" return _category;");
                    sb.AppendLine("}");
                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// 修改一个" + _table.Description + "信息");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine("public void EditOne" + _table.TableName + "()");
                    sb.AppendLine("{");
                    sb.AppendLine("this.Edit();");
                    sb.AppendLine("List<" + _table.TableName + "> _categorys = GetInstance();");
                    sb.AppendLine("var query = _categorys.Where(a => a.Id == this.Id).ToList();");
                    sb.AppendLine("foreach (" + _table.TableName + " c in query)");
                    sb.AppendLine("{");
                    sb.AppendLine("_categorys.Remove(c);");
                    sb.AppendLine(" }");
                    sb.AppendLine("_categorys.Add(GetSelf());");
                    sb.AppendLine("lock (obj)");
                    sb.AppendLine("{");
                    sb.AppendLine("infos = _categorys;");
                    sb.AppendLine("}");
                    sb.AppendLine(" }");
                    sb.AppendLine("/// <summary>");
                    sb.AppendLine("/// 删除一个" + _table.Description + "信息");
                    sb.AppendLine("/// </summary>");
                    sb.AppendLine(" public void DeleteOne" + _table.TableName + "()");
                    sb.AppendLine("{");
                    sb.AppendLine("this.Delete();");
                    sb.AppendLine("List<" + _table.TableName + "> _categorys = GetInstance();");
                    sb.AppendLine("var query = _categorys.Where(a => a.Id == this.Id).ToList();");
                    sb.AppendLine("lock (obj)");
                    sb.AppendLine("{");
                    sb.AppendLine("foreach (var category in query)");
                    sb.AppendLine("{");
                    sb.AppendLine("infos.Remove(category);");
                    sb.AppendLine("}");
                    sb.AppendLine("}");
                    sb.AppendLine("}");

                }
                sb.AppendLine("        #endregion");
                sb.AppendLine("");
                sb.AppendLine("        #region 自定义有效方法");
                sb.AppendLine("");
                sb.AppendLine("        #endregion");
                sb.AppendLine("");
                sb.AppendLine("        #region 自定义方法，已过时，不在使用");
                sb.AppendLine("");
                sb.AppendLine("        #endregion");
                sb.AppendLine("");
                sb.AppendLine("    }");
                sb.AppendLine("}");
            }
            return sb.ToString();
        }
    }
}
